package parser;

import antlr.RecognitionException;
import java.util.*;

class WhileStmt extends LoopStmt {
    
    Expr cond; 
    
    WhileStmt(Expr cond) {
        this.cond = cond;
    }
    
    void addElse(Stmt suite) {
        throw new Error("Not implemented");
    }
    
    void write(ByteStream out) throws RecognitionException {
        // Place condition at start AND end. Improves tracing
        // code quality as it helps type-stability at end of loop.
        // Traced loops will include test.
        out.line(line);
//        out.write(Opcodes.WHILE);
        Patch patch = cond.branch(false, out);
        int start = out.here();
        suite.write(out);
        int test = out.here();
        for (Patch c : continues) {
           out.patch(c);
        }
        out.line(line);
        Patch patch2 = cond.branch(false, out);
        out.branch_back(Opcodes.END_LOOP, start);
        out.patch(patch);
        out.patch(patch2);
        for (Patch b : breaks) {
           out.patch(b);
        }
    }

}
